package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.FamMember;
import com.ruoyi.system.service.IFamMemberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 家谱成员Controller
 * 
 * @author wk
 * @date 2022-05-19
 */
@RestController
@RequestMapping("/system/member")
public class FamMemberController extends BaseController
{
    @Autowired
    private IFamMemberService famMemberService;
    @Autowired
    private IFamMemberService maleService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询家谱成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(FamMember famMember)
    {
        startPage();
        List<FamMember> list = famMemberService.selectFamMemberList(famMember);
        return getDataTable(list);
    }

    /**
     * 导出家谱成员列表
     */
    @PreAuthorize("@ss.hasPermi('system:member:export')")
    @Log(title = "家谱成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FamMember famMember)
    {
        List<FamMember> list = famMemberService.selectFamMemberList(famMember);
        ExcelUtil<FamMember> util = new ExcelUtil<FamMember>(FamMember.class);
        util.exportExcel(response, list, "家谱成员数据");
    }

    /**
     * 获取家谱成员详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:member:query')")
    @GetMapping(value ={"/","/{memberId}"} )
    public AjaxResult getInfo(@PathVariable(value="memberId",required = false) Long memberId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("males", maleService.selectmaleAll());
        ajax.put("females", maleService.selectfemaleAll());
        if (StringUtils.isNotNull(memberId))
        {
            FamMember famMember = famMemberService.selectFamMemberByMemberId(memberId);
            ajax.put(AjaxResult.DATA_TAG, famMember);
            ajax.put("males", maleService.selectmaleAll());
            ajax.put("females", maleService.selectfemaleAll());
        }
        return ajax;
    }

    /**
     * 新增家谱成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:add')")
    @Log(title = "家谱成员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FamMember famMember)
    {
        return toAjax(famMemberService.insertFamMember(famMember));
    }

    /**
     * 修改家谱成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:edit')")
    @Log(title = "家谱成员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FamMember famMember)
    {
        return toAjax(famMemberService.updateFamMember(famMember));
    }

    /**
     * 删除家谱成员
     */
    @PreAuthorize("@ss.hasPermi('system:member:remove')")
    @Log(title = "家谱成员", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(famMemberService.deleteFamMemberByMemberIds(memberIds));
    }

    /**
     * 获取分支下拉树列表
     */
    @GetMapping("/memberTreeselect")
    public AjaxResult memberTreeselect(FamMember member)
    {

        Long userId = userService.getUserIdByUserName(getUsername());
//        SysDept dept;
//        dept.setUserId(userId);
        List<FamMember> members = famMemberService.selectFamMemberList(member);
        return AjaxResult.success(famMemberService.buildMemberTreeSelect(members));


    }
}
